package config;

import java.awt.Color;

import ui.Window;
import define.Common;
import define.Config;
import define.ErrorDefine;
import main.ErrorException;

public enum ConfigType
{
	E_FONT_IMAGE_SIZE_CT("cfis", "1024", ConfigValueType.E_INT_CVT, Config.MENU_FONT_GROUP, "font image size"),
	E_FONT_BG_COLOR_CT("cfbg", "ff000000", ConfigValueType.E_LONG_CVT, Config.MENU_FONT_GROUP, "font background color"),
	E_FONT_FG_COLOR_CT("cffg", "ffffffff", ConfigValueType.E_LONG_CVT, Config.MENU_FONT_GROUP, "font frontground color"),
	E_FONT_PADDING_CT("cfp", "0", ConfigValueType.E_INT_CVT, Config.MENU_FONT_GROUP, "font padding"),
	E_FONT_WHOLE_CT("cfsbc", "true", ConfigValueType.E_BOOL_CVT, Config.MENU_FONT_GROUP, "using sbc"),
	E_FONT_BOLD_CT("cfb", "false", ConfigValueType.E_BOOL_CVT, Config.MENU_FONT_GROUP, "font sytle bold"),
	E_FONT_ITALIC_CT("cfsi", "false", ConfigValueType.E_BOOL_CVT, Config.MENU_FONT_GROUP, "font style italic"),
	
	E_OUT_CHARSET_CT("cocs", "asc", ConfigValueType.E_STRING_CVT, Config.MENU_OUTPUT_GROUP, "output charset"),
	
	E_ATLAS_BG_COLOR_CT("cabg", "0", ConfigValueType.E_LONG_CVT, Config.MENU_ATLAS_GROUP, "atlas background color"),
	E_ATLAS_SIZE_CT("cais", "1024", ConfigValueType.E_INT_CVT, Config.MENU_ATLAS_GROUP, "atlas image size"),
	E_ATLAS_PADDING_CT("cap", "0", ConfigValueType.E_INT_CVT, Config.MENU_ATLAS_GROUP, "atlas padding"),
	;
	
	private String m_name;
	private String m_value;
	private ConfigValueType m_type;
	private int m_group;
	private String m_menuText;
	
	private ConfigType(String name, String defaultValue, ConfigValueType type, int group, String menuText)
	{
		m_name = name;
		m_value = defaultValue;
		m_type = type;
		m_group = group;
		m_menuText = menuText;
	}
	
	public int getGroup()
	{
		return m_group;
	}
	
	public String getMenuShowText()
	{
		return m_name + " " + m_menuText + " " + m_value;
	}
	
	public String getPopupShowText()
	{
		return m_menuText;
	}
	
	public int getIntValue16()
	{
		return Integer.parseInt(m_value, 16);
	}
	
	public int getIntValue()
	{
		return Integer.parseInt(m_value);
	}
	
	public String getStringValue()
	{
		return m_value;
	}
	
	public long getLongValue()
	{
		return Long.parseLong(m_value, 16);
	}
	
	public Color createColor(long value)
	{
		int r = (int)((value >> 16) & 0xff);
		int g = (int)((value >> 8) & 0xff);
		int b = (int)(value & 0xff);
		int a = (int)((value >> 24) & 0xff);
		return new Color(r, g, b, a);
	}
	
	public static ConfigType getConfig(String name)
	{
		for(ConfigType ct: ConfigType.values())
		{
			if(ct.m_name.compareTo(name) == 0)
			{
				return ct;
			}
		}
		return null;
	}
	
	public void setValue(String value, int position) throws ErrorException
	{
		Object ob = m_type.checkType(value);
		if(ob == null)
		{
			throw new ErrorException(ErrorDefine.E_CONFIG_TYPE_ERROR, m_type.getText() + "," + value, position);
		}
		boolean isPass = true;
		switch(this)
		{
		case E_FONT_BG_COLOR_CT:
		case E_FONT_FG_COLOR_CT:
			long l = (long)ob;
			isPass = l >= 0;
			break;
		case E_FONT_IMAGE_SIZE_CT:
		case E_FONT_PADDING_CT:
			int i = (int)ob;
			isPass = i >= 0;
			break;
		case E_OUT_CHARSET_CT:
			if(OutputCharset.getCharset(value) == null)
			{
				throw new ErrorException(ErrorDefine.E_OUTPUT_CHARSET_ERROR, OutputCharset.getErrorText(), position);
			}
			isPass = true;
			break;
		case E_FONT_WHOLE_CT:
		default:
			break;
		}
		if(isPass)
		{
			m_value = value;
			if(Common.m_hasUI)
			{
				Window.m_instance.changeMenuText(this);
			}
		}
		else
		{
			throw new ErrorException(ErrorDefine.E_ILLEGAL_INPUT_ERROR, value, position);
		}
	}
	
	public boolean getBoolValue()
	{
		return m_value.compareTo("true") == 0;
	}
}
